//
//  ZYCCircleFriendPhotoView.m
//  ZDSC
//
//  Created by ZDTX on 2021/9/19.
//

#import "ZYCCircleFriendPhotoView.h"
#import "ZYCCycleFriendModel.h"
#import <UIImageView+WebCache.h>

#define imageW 40

@interface ZYCCircleFriendPhotoView ()
@property (nonatomic,strong)UIImageView *firstImageView;
@property (nonatomic,strong)UIImageView *secondImageView;
@property (nonatomic,strong)UIImageView *thirdImageView;
@property (nonatomic,strong)UIImageView *fourImageView;

@end
@implementation ZYCCircleFriendPhotoView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.firstImageView];
        [self addSubview:self.secondImageView];
        [self addSubview:self.thirdImageView];
        [self addSubview:self.fourImageView];
        [self.firstImageView mas_makeConstraints:^(MASConstraintMaker *make) {
           make.size.mas_equalTo(CGSizeMake(imageW,imageW));
            make.left.top.equalTo(self);
        }];
        [self.secondImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(imageW,imageW));
            make.right.top.equalTo(self);
        }];
        [self.thirdImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(imageW,imageW));
            make.left.bottom.equalTo(self);
        }];
        [self.fourImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(imageW,imageW));
            make.right.bottom.equalTo(self);
        }];
    }
    return self;
}


- (void)setBusFileList:(NSArray<ZYCCyclePhotoModel *> *)busFileList{
    _busFileList = busFileList;
    if (busFileList.count == 1) {
        self.firstImageView.hidden = NO;
        self.secondImageView.hidden = YES;
        self.thirdImageView.hidden = YES;
        self.fourImageView.hidden = YES;
        [self.firstImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
    }else if(busFileList.count == 2){
        self.firstImageView.hidden = NO;
        self.secondImageView.hidden = NO;
        self.thirdImageView.hidden = YES;
        self.fourImageView.hidden = YES;

        [self.firstImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(imageW,imageW * 2));
        }];
        [self.secondImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.top.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(imageW,imageW * 2));
        }];
    }else if(busFileList.count == 3){
        self.firstImageView.hidden = NO;
        self.secondImageView.hidden = NO;
        self.thirdImageView.hidden = NO;
        self.fourImageView.hidden = YES;

        [self.firstImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(imageW,imageW * 2));
        }];
        [self.secondImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.firstImageView.mas_right);
            make.top.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(imageW,imageW));
        }];
        [self.thirdImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.firstImageView.mas_right);
            make.bottom.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(imageW,imageW));
        }];

    }else{
        self.firstImageView.hidden = NO;
        self.secondImageView.hidden = NO;
        self.thirdImageView.hidden = NO;
        self.fourImageView.hidden = NO;
        [self.firstImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(imageW,imageW));
            make.left.top.equalTo(self);
        }];
        [self.secondImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(imageW,imageW));
            make.right.top.equalTo(self);
        }];
        [self.thirdImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(imageW,imageW));
            make.left.bottom.equalTo(self);
        }];
        [self.fourImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(imageW,imageW));
            make.right.bottom.equalTo(self);
        }];


    }
    
    [busFileList enumerateObjectsUsingBlock:^(ZYCCyclePhotoModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/zd-system-service%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"sdfz_url"],obj.filePath]];
        if (idx == 0) {
            [self.firstImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder_image_icon"]];
        }else if(idx == 1){
            [self.secondImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder_image_icon"]];
        }else if(idx == 2){
            [self.thirdImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder_image_icon"]];
        }else{
            [self.fourImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder_image_icon"]];
        }
    }];
    


}


- (UIImageView *)firstImageView {
    if (!_firstImageView) {
        _firstImageView = [[UIImageView alloc] init];
        _firstImageView.contentMode = UIViewContentModeScaleToFill;
        _firstImageView.hidden = YES;
    }
    return _firstImageView;
}

- (UIImageView *)secondImageView {
    if (!_secondImageView) {
        _secondImageView = [[UIImageView alloc] init];
        _secondImageView.contentMode = UIViewContentModeScaleToFill;
        _secondImageView.hidden = YES;
    }
    return _secondImageView;
}
- (UIImageView *)thirdImageView {
    if (!_thirdImageView) {
        _thirdImageView = [[UIImageView alloc] init];
        _thirdImageView.contentMode = UIViewContentModeScaleToFill;
        _thirdImageView.hidden = YES;
    }
    return _thirdImageView;
}
- (UIImageView *)fourImageView {
    if (!_fourImageView) {
        _fourImageView = [[UIImageView alloc] init];
        _fourImageView.contentMode = UIViewContentModeScaleToFill;
        _fourImageView.hidden = YES;
    }
    return _fourImageView;
}

@end
